from django.db import models

# Create your models here.
from question.models import Label
from users.models import User


class BaseModel(models.Model):
    """为模型类补充字段"""
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        abstract = True  # 说明是抽象模型类, 用于继承使用，数据库迁移时不会创建BaseModel的表


class Category(BaseModel):
    """新闻专栏"""
    STATUS_CHOICES = (
        (1, "审核通过"),
        (2, "待审核"),
        (3, "审核中")
    )
    name = models.CharField(max_length=16, unique=True, verbose_name='专栏名')
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=2, verbose_name='专栏状态')
    sequence = models.IntegerField( unique=True, verbose_name='专栏顺序')
    url = models.CharField(max_length=64, null=True, verbose_name='专栏页面链接')
    description = models.CharField(max_length=64, null=True, verbose_name='专栏简介')

    class Meta:
        db_table = 'tb_category'
        verbose_name = '新闻专栏'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class News(BaseModel):
    """新闻详情模型类"""
    STATUS_CHOICES = (
        (1, "审核通过"),
        (2, "待审核"),
        (3, "审核中")
    )
    category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name="新闻分类")
    author = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="新闻作者")
    title = models.CharField(max_length=64, verbose_name="新闻标题")
    content = models.TextField(verbose_name="新闻内容")
    digest = models.CharField(max_length=64, verbose_name="新闻摘要")
    source = models.CharField(max_length=16, verbose_name="新闻来源")
    comment_count = models.IntegerField(default=0, verbose_name="新闻评论量")
    click_count = models.IntegerField(default=0, verbose_name="新闻点击量")
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=2, verbose_name='新闻状态')
    is_comment = models.BooleanField(default=True, verbose_name='是否可以评论')
    is_stick = models.BooleanField(default=False, verbose_name='是否置顶')
    label = models.ManyToManyField(Label, verbose_name="新闻标签")

    class Meta:
        db_table = 'tb_news_detail'
        verbose_name = '新闻详情'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Comment(BaseModel):
    """新闻评论表"""
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='评论用户')
    news = models.ForeignKey(News, on_delete=models.PROTECT, verbose_name='评论新闻')
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='父评论')
    content = models.TextField(default='', verbose_name="评论内容")

    class Meta:
        db_table = 'tb_comment'
        verbose_name = '新闻评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class Advertisement(BaseModel):
    """广告模型类"""
    image_url = models.CharField(max_length=64, verbose_name="广告图片")
    url = models.CharField(max_length=64, verbose_name="推送链接")

    class Meta:
        db_table = 'tb_advertisements'
        verbose_name = '广告'
        verbose_name_plural = verbose_name
